package cn.tedu.management.portal.service.impl;

import cn.tedu.management.portal.ex.InsertException;
import cn.tedu.management.portal.model.Job;
import cn.tedu.management.portal.mapper.JobMapper;
import cn.tedu.management.portal.service.IJobService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tedu.cn
 * @since 2020-12-08
 */
@Service
public class JobServiceImpl extends ServiceImpl<JobMapper, Job> implements IJobService {

    @Autowired
    JobMapper jobMapper;

    @Override
    public List<Job> getJobList(){
        return jobMapper.findAll();
    }

    @Override
    public void insert(String jobName){
        Job job = new Job();
        job.setJobName(jobName);
        LocalDateTime now = LocalDateTime.now();
        job.setGmtCreate(now);
        job.setGmtModified(now);
        int rows = jobMapper.insert(job);
        if (rows != 1){
            throw new InsertException("添加失败,请稍后再次尝试!");
        }
    }
    @Override
    public void delete(Integer id){
        Job job = new Job();
        job.setId(id);
        int rows = jobMapper.deleteById(id);
        if ( rows == 0){
            throw new InsertException("删除失败该用户不存在!");
        }
    }
    @Override
    public void update(Integer id,String jobName){
        Job job = new Job();
        job.setId(id);
        job.setJobName(jobName);
        LocalDateTime now = LocalDateTime.now();
        job.setGmtModified(now);
        int rows = jobMapper.updateById(job);
        if (rows == 0 ){
            throw new InsertException("修改失败");
        }
    }
}
